package com.javaEE.tes.controllers;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.javaEE.tes.dao.AssociationRepository;
import com.javaEE.tes.dao.GenderRepository;
import com.javaEE.tes.dao.StudentRepository;
import com.javaEE.tes.dao.TeacherRepository;
import com.javaEE.tes.dao.dto.CharData;
import com.javaEE.tes.dao.dto.StudentAssociation;
import com.javaEE.tes.dao.dto.StudentClass1;
import com.javaEE.tes.entities.Association;
import com.javaEE.tes.entities.Student;
import com.javaEE.tes.entities.Teacher;
import com.javaEE.tes.services.AssociationService;
import com.javaEE.tes.services.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class HomerControllers {
    @Autowired
    AssociationService associationService;
    @Autowired
    StudentService studentService;
    @Autowired
    TeacherRepository teacherRepository;
    @Autowired
    GenderRepository genderRepository;

    @GetMapping("/")
    public String displayHome(Model model) throws JsonProcessingException{

        List<Student> students=studentService.getAll();
        model.addAttribute("students",students);

        List<Association> associations=associationService.getAll();
        model.addAttribute("association",associations);

        List<Teacher> teachers = teacherRepository.findAll();
        model.addAttribute("teacher",teachers);


        List<CharData> teacherEducation = teacherRepository.getTeacherEducation();
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(teacherEducation);
        model.addAttribute("teacherEducation",jsonString);

        List<CharData> studentGender = genderRepository.getGender();
        ObjectMapper objectMapper1 = new ObjectMapper();
        String jsonString1 = objectMapper1.writeValueAsString(studentGender);
        model.addAttribute("studentGender",jsonString1);
        return "main/home";
    }
}
